﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Collections;
using Tamir.SharpSsh;

namespace Calc
{
    public partial class Form1 : Form
    {
        SshConnectionInfo info = new SshConnectionInfo();
        SshShell shell;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string output;
            info.Host = serverTextBox.Text;
            info.User = usernameTextBox.Text;
            info.Pass = passwordTextBox.Text;
            info.IdentityFile = "y";
            shell = new SshShell(info.Host, info.User);
            shell.RemoveTerminalEmulationCharacters = true;
            
            if (info.Pass != null) 
            { 
                shell.Password = info.Pass; 
            }
            if (info.IdentityFile != null)
            {
                //shell.AddIdentityFile(info.IdentityFile);
            }

            shell.Connect();
            output = shell.Expect();
            cmdTextBox.Text += output;

            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();

            shell.WriteLine("pwd");
            Thread.Sleep(500);
            output = shell.Expect();
            cmdTextBox.Refresh();
            cmdTextBox.Text += output;

            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();

        }

        public struct SshConnectionInfo
        {
            public string Host;
            public string User;
            public string Pass;
            public string IdentityFile;
        }

        private void SendCommandTextBox_Click(object sender, EventArgs e)
        {
            string output;
            if(commandTextBox.Text != "exit")
            {
                shell.WriteLine(commandTextBox.Text);
                Thread.Sleep(500);
                output = shell.Expect();
                cmdTextBox.Text += output;

                cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
                cmdTextBox.ScrollToCaret();
                cmdTextBox.Refresh();

                commandTextBox.Text = String.Empty;
                //cmdTextBox.Text += Environment.NewLine;
                //output = shell.RunCommand(commandTextBox.Text);
                //cmdTextBox.Text += output.Replace("\n",Environment.NewLine);
                //cmdTextBox.Text += shell.RunCommand("pwd");
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {

                shell.Close();
            }
            catch
            {
            }
        }

        private void commandTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendCommandTextBox_Click(sender, e);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void number1_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "1";
        }

        private void number2_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "2";
        }

        private void number3_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "3";
        }

        private void number4_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "4";
        }

        private void number5_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "5";
        }

        private void number6_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "6";
        }

        private void number7_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "7";
        }

        private void number8_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "8";
        }

        private void number9_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "9";
        }

        private void number0_Click(object sender, EventArgs e)
        {
            commandTextBox.Text += "0";
        }

        private void op_plus_Click(object sender, EventArgs e)
        {
            string output;
            SendCommandTextBox_Click(sender, e);
            shell.WriteLine("+");

            Thread.Sleep(500);
            output = shell.Expect();
            cmdTextBox.Text += output;

            //move text box so we can see it
            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();
            cmdTextBox.Refresh();

            commandTextBox.Text = String.Empty;
        }

        private void op_subtract_Click(object sender, EventArgs e)
        {
            string output;
            SendCommandTextBox_Click(sender, e);
            shell.WriteLine("-");

            Thread.Sleep(500);
            output = shell.Expect();
            cmdTextBox.Text += output;

            //move text box so we can see it
            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();
            cmdTextBox.Refresh();

            commandTextBox.Text = String.Empty;
        }

        private void op_mul_Click(object sender, EventArgs e)
        {
            string output;
            SendCommandTextBox_Click(sender, e);
            shell.WriteLine("*");

            Thread.Sleep(500);
            output = shell.Expect();
            cmdTextBox.Text += output;

            //move text box so we can see it
            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();
            cmdTextBox.Refresh();

            commandTextBox.Text = String.Empty;
        }

        private void op_div_Click(object sender, EventArgs e)
        {
            string output;
            SendCommandTextBox_Click(sender, e);
            shell.WriteLine("/");

            Thread.Sleep(500);
            output = shell.Expect();
            cmdTextBox.Text += output;

            //move text box so we can see it
            cmdTextBox.SelectionStart = cmdTextBox.Text.Length;
            cmdTextBox.ScrollToCaret();
            cmdTextBox.Refresh();

            commandTextBox.Text = String.Empty;
        }

        private void op_equal_Click(object sender, EventArgs e)
        {
            SendCommandTextBox_Click(sender, e);

           
        }

        private void clearCommandWindow_Click(object sender, EventArgs e)
        {
             cmdTextBox.Text = String.Empty;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }




    }
}
